package image.generators;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

public class ShapesGenerators {
	/**
	 * Generates a rectangle at center x,y and others params given
	 * @param centerX
	 * @param centerY
	 * @param widthRectangle
	 * @param heightRectangle
	 * @param widthImage
	 * @param heightImage
	 * @return
	 */
	public static BufferedImage generateRectangle(int centerX, int centerY,
			int widthRectangle, int heightRectangle, int widthImage,
			int heightImage) {
		BufferedImage ret = new BufferedImage(heightImage, widthImage,
				BufferedImage.TYPE_BYTE_GRAY);
		Graphics2D g2d = (Graphics2D) ret.getGraphics();
		Point2D start = new Point2D.Float((float) centerX - widthRectangle / 2,(float) centerY - heightRectangle / 2);
		Rectangle2D rectangle = new Rectangle2D.Float((float)start.getX(),(float)start.getY(),(float) widthRectangle, (float) heightRectangle);
	//	Rectangle2D rectangle = new Rectangle2D.Float(centerX,centerY,(float) widthRectangle, (float) heightRectangle);
		g2d.setPaint(Color.WHITE);
		g2d.fill(rectangle);
		g2d.draw(rectangle);
		return ret;
	}

	/**
	 * Generate rectangle at center of the image
	 * @param widthRectangle
	 * @param heightRectangle
	 * @param widthImage
	 * @param heightImage
	 * @return
	 */
	public static BufferedImage generateRectangle(int widthRectangle,
			int heightRectangle, int widthImage, int heightImage) {

		int centerX = (int) Math.floor(((float) widthImage / 2));
		int centerY = (int) Math.floor(((float) heightImage / 2));

		return generateRectangle(centerX,centerY,widthRectangle,heightRectangle,widthImage,heightImage);
	}

	public static BufferedImage generateSquare(int widthAndHeight,
			int widthImage, int heightImage) {
		return generateRectangle(
				(int) Math.floor(((float) widthAndHeight / 2)),
				(int) Math.floor(((float) widthAndHeight / 2)), widthImage,
				heightImage);
	}

	public static BufferedImage generateRectangleRotated(int widthRectangle,
			int heightRectangle, int widthImage, int heightImage, double theta) {
		BufferedImage ret = new BufferedImage(heightImage, widthImage,
				BufferedImage.TYPE_BYTE_GRAY);
		Graphics2D g2d = (Graphics2D) ret.getGraphics();
		int centerX = (int) Math.floor(((float) widthImage / 2));
		int centerY = (int) Math.floor(((float) heightImage / 2));
		
		Point2D start = new Point2D.Float((float) centerX - widthRectangle / 2,(float) centerY - heightRectangle / 2);

		
		g2d.rotate(theta, start.getX() + heightRectangle / 2 + widthRectangle / 2, start.getY()+ heightRectangle / 2 + widthRectangle / 2);
		Rectangle2D rectangle = new Rectangle2D.Float((float)start.getX(), (float)start.getY(),
				(float) widthRectangle, (float) heightRectangle);
		g2d.fill(rectangle);
		g2d.draw(rectangle);
		return ret;
	}

	public static BufferedImage generateLines(int linesSpace, int widthImage,
			int heightImage) {
		BufferedImage ret = new BufferedImage(heightImage, widthImage,
				BufferedImage.TYPE_BYTE_GRAY);
		for (int y = 0; y < ret.getHeight(); y++) {
			if (y % linesSpace == 0) {
				for (int x = 0; x < ret.getWidth(); x++) {
					ret.getRaster().setSample(x, y, 0, 255);
				}
			}
		}

		return ret;

	}

	public static BufferedImage generateDiagonalLines(int linesSpace,
			int widthImage, int heightImage) {
		BufferedImage ret = new BufferedImage(heightImage, widthImage,
				BufferedImage.TYPE_BYTE_GRAY);
		Graphics2D g2d = (Graphics2D) ret.getGraphics();

		for (int y = 0; y < ret.getHeight(); y++) {
			if (y % linesSpace == 0) {
				Line2D line = new Line2D.Float((float) y, 0f, 0f, (float) y);
				g2d.setStroke(new BasicStroke(2));
				g2d.draw(line);
			}
		}

		float y = 0;
		for (int x = 0; x < ret.getWidth(); x++) {
			if (x % linesSpace == 0) {
				Point2D start = new Point2D.Float(x, ret.getHeight());
				Point2D end = new Point2D.Float(ret.getWidth(), y);
				Line2D line = new Line2D.Float(start, end);
				g2d.setStroke(new BasicStroke(2));
				g2d.draw(line);
				y += linesSpace;
			}
		}

		return ret;

	}

	public static BufferedImage drawCircle(int a, int b, int r, int widthImage,
			int heightImage) {
		BufferedImage ret = new BufferedImage(heightImage, widthImage,
				BufferedImage.TYPE_BYTE_GRAY);
		Graphics2D g2 = (Graphics2D) ret.getGraphics();
		int xstart = a - r;
		int xend = a + r;
		int ystart = b - r;
		int yend = b + r;

		Ellipse2D e = new Ellipse2D.Double(xstart, ystart, Math.abs(xstart
				- xend), Math.abs(ystart - yend));
		g2.setPaint(Color.WHITE);
		g2.setStroke(new BasicStroke(1));
		g2.fill(e);
		g2.draw(e);
		return ret;
	}

}
